package com.fita.admin.web.controller.finance;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.report.WithdrawRecordSearch;
import com.fita.admin.service.agent.AgentService;
import com.fita.admin.service.report.WithdrawRecordService;
import com.fita.admin.web.controller.view.Excel2TemplateView;
import com.fita.entity.agent.Agent;
import com.fita.entity.order.OrderPayType;
import com.fita.entity.wallet.WithdrawRecord;
import com.fita.entity.wallet.WithdrawRecordPaidStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 *  提现记录
 */
@Controller
@RequestMapping(path = "/withdraw-records")
@MenuSetting("menu-report")
public class WithdrawRecordController {

    private final static Logger logger = LoggerFactory.getLogger(WithdrawRecordController.class);

    @Autowired
    private WithdrawRecordService withdrawRecordService;

    @Autowired
    private AgentService agentService;

    /**
     *
     * @return
     */
    @GetMapping(path = "/search")
    public ModelAndView search(WithdrawRecordPaidStatus withdrawRecordPaidStatus) {

        ModelAndView modelAndView = new ModelAndView("report/withdraw-record/withdraw-record-search");

        List<Agent> agents = agentService.listAll(Agent.class);

        modelAndView.addObject("agents", agents);

        modelAndView.addObject("withdrawRecordPaidStatus", withdrawRecordPaidStatus);

        return modelAndView;
    }

    /**
     *
     * @param page
     * @param withdrawRecordSearch
     * @param request
     * @return
     */
    @GetMapping(path = "")
    @MenuSetting("submenu-withdraw-record-list")
    public ModelAndView list(Page page,
                             WithdrawRecordSearch withdrawRecordSearch,
                             HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView("report/withdraw-record/withdraw-record-" + withdrawRecordSearch.getWithdrawRecordPaidStatus().toString().toLowerCase() + "-list");

        modelAndView.addObject("withdrawRecordPaidStatus", withdrawRecordSearch.getWithdrawRecordPaidStatus());

        int count = withdrawRecordService.count(withdrawRecordSearch);

        if (count > 0){

            List<WithdrawRecord> withdrawRecords = withdrawRecordService.search(WithdrawRecord.class,withdrawRecordSearch , page.with(count));

            modelAndView.addObject("withdrawRecords", withdrawRecords);

            modelAndView.addObject("page", page);

            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
        }

        return modelAndView;
    }

    /**
     *
     * @param withdrawRecordId
     * @return
     */
    @GetMapping(path = "/{withdrawRecordId}")
    @MenuSetting("submenu-withdraw-record-list")
    public ModelAndView view (@PathVariable Long withdrawRecordId){

        ModelAndView modelAndView = new ModelAndView("report/withdraw-record/withdraw-record-view");

        WithdrawRecord withdrawRecord = withdrawRecordService.load(WithdrawRecord.class, withdrawRecordId);

        modelAndView.addObject("withdrawRecord", withdrawRecord);

        return modelAndView;
    }

    /**
     * 提现同意页面
     * @param withdrawRecordId
     * @return
     */
    @GetMapping(path = "/{withdrawRecordId}/confirm")
    public ModelAndView confirm(@PathVariable Long withdrawRecordId){

        ModelAndView modelAndView = new ModelAndView("report/withdraw-record/withdraw-record-withdraw-confirm");

        WithdrawRecord withdrawRecord = withdrawRecordService.load(WithdrawRecord.class, withdrawRecordId);

        modelAndView.addObject("withdrawRecord", withdrawRecord);

        return modelAndView;
    }

    /**
     * 提现同意
     * @return
     */
    @PostMapping(path = "/{withdrawRecordId}/confirm", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult onConfirm(@PathVariable Long withdrawRecordId,
                                @RequestParam OrderPayType payType,
                                @RequestParam String payCode){

        HttpResult httpResult = new HttpResult();
        try {
            withdrawRecordService.confirm(withdrawRecordId, payType, payCode);
            return new HttpResult().success().message("操作成功");

        } catch (BusinessException e){
            logger.info("#onConfirm() info [{}]", e);
            return new HttpResult().fail("500").message(e.getMessage());
        } catch (Exception e) {
            logger.error("#onConfirm() error [{}]", e);
            return new HttpResult().fail("500").message("操作失败");
        }
    }

    @GetMapping(path = "/export")
    public ModelAndView export(@RequestParam(defaultValue = "UNPAID") WithdrawRecordPaidStatus withdrawRecordPaidStatus){

        ModelAndView modelAndView = new ModelAndView("report/withdraw-record/withdraw-record-export");

        List<Agent> agents = agentService.listAll(Agent.class);

        modelAndView.addObject("agents", agents);

        modelAndView.addObject("withdrawRecordPaidStatus", withdrawRecordPaidStatus);

        return modelAndView;
    }

    @PostMapping(path = "/export")
    public ModelAndView onExport(WithdrawRecordSearch withdrawRecordSearch, Page page){

        ModelAndView modelAndView = new ModelAndView(new Excel2TemplateView("report/withdraw-record-report.xlsx"));

        int count = withdrawRecordService.count(withdrawRecordSearch);

        if (count > 0){

            List<WithdrawRecord> withdrawRecords = withdrawRecordService.search(WithdrawRecord.class, withdrawRecordSearch, page.with(count));

            modelAndView.addObject("withdrawRecords", withdrawRecords);
        }

        modelAndView.addObject("fmt", new SimpleDateFormat("yyyy-MM-dd HH:mm"));

        return modelAndView;
    }

}
